package com.neusoft.elmboot.service.impl;

import com.neusoft.elmboot.common.Constants;
import com.neusoft.elmboot.dto.PermVo;
import com.neusoft.elmboot.dto.RoleReqDto;
import com.neusoft.elmboot.mapper.RolePermissionMapper;
import com.neusoft.elmboot.po.Role;
import com.neusoft.elmboot.mapper.RoleMapper;
import com.neusoft.elmboot.po.RolePermission;
import com.neusoft.elmboot.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import java.util.*;

/**
 * 角色表(Role)表服务实现类
 *
 * @author makejava
 * @since 2024-03-23 15:14:31
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Role queryById(Integer id) {
        return this.roleMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param role 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Role> queryByPage(Role role, PageRequest pageRequest) {
        long total = this.roleMapper.count(role);
        return new PageImpl<>(this.roleMapper.queryAllByLimit(role, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param roleReqDto 实例对象
     * @return 实例对象
     */
    @Override
    public Role insert(RoleReqDto roleReqDto) {
        Role r = new Role();
        BeanUtils.copyProperties(roleReqDto,r);
        this.roleMapper.insert(r);
        return r;
    }

    /**
     * 修改数据
     *
     * @param roleDto 实例对象
     * @return 实例对象
     */
    @Override
    public Role update(RoleReqDto roleDto) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDto,role);
        this.roleMapper.update(role);
        return this.queryById(role.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.roleMapper.deleteById(id) > 0;
    }

    @Override
    public List<Role> query(RoleReqDto roleDto) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDto,role);
        return this.roleMapper.queryAllByLimit(role, null);
    }

    @Override
    public List<Integer> queryPermIdsByRoleId(Integer roleId, List<PermVo> permVos) {
        List<Integer> permIds = new ArrayList<>();
        if(Constants.SUPER_ADMIN_ROLE_ID == roleId) {
            permVos.forEach(p -> {
                permIds.add(Integer.parseInt(p.getId()));
            });
            return permIds;
        }
        List<RolePermission> rolePermissions = rolePermissionMapper.selectByRoleId(roleId);

        rolePermissions.forEach(m -> permIds.add(m.getPermissionId()));
        return permIds;
    }

    @Override
    public boolean savePermissions(Integer roleId, List<Integer> permissions) {
        rolePermissionMapper.deleteByRoleId(roleId);
        Map<String, Object> params = new HashMap<>();
        params.put("roleId",roleId);
        params.put("permIds",permissions);
        int x = rolePermissionMapper.insertBatch(params);
        return x > 0 ? true : false ;
    }


    @Override
    public List<Role> queryAllRole() {
        return roleMapper.queryAllByLimit(new Role(), null);
    }
}
